use num::complex::Complex;
use std::collections::HashMap;
pub fn hello() {
    println!("==========base::hello.rs===========");
    println!("==========基本类型===========");
    // 加法
    // let sum = 5 + 10;

    // 减法
    // let difference = 95.5 - 4.3;

    // 乘法
    // let product = 4 * 30;

    // 除法
    // let quotient = 56.7 / 32.2;

    // 求余
    // let remainder = 43 % 5;
    // 编译器会进行自动推导，给予twenty i32的类型
    let twenty = 20;
    // 类型标注
    let twenty_one: i32 = 21;
    // 通过类型后缀的方式进行类型标注：22是i32类型
    let twenty_two = 22i32;

    // 只有同样类型，才能运算
    let addition = twenty + twenty_one + twenty_two;
    println!(
        "{} + {} + {} = {}",
        twenty, twenty_one, twenty_two, addition
    );

    // 对于较长的数字，可以用_进行分割，提升可读性
    let one_million: i64 = 1_000_000;
    println!("{}", one_million.pow(2));

    // 定义一个f32数组，其中42.0会自动被推导为f32类型
    let forty_twos = [42.0, 42f32, 42.0_f32];

    // 打印数组中第一个值，并控制小数位为2位
    println!("{:.2}", forty_twos[0]);
    // 二进制为00000010
    let a: i32 = 2;
    // 二进制为00000011
    let b: i32 = 3;

    println!("(a & b) value is {}", a & b);

    println!("(a | b) value is {}", a | b);

    println!("(a ^ b) value is {}", a ^ b);

    println!("(!b) value is {} ", !b);

    println!("(a << b) value is {}", a << b);

    println!("(a >> b) value is {}", a >> b);

    let mut a = a;
    // 注意这些计算符除了!之外都可以加上=进行赋值 (因为!=要用来判断不等于)
    a <<= b;
    println!("(a << b) value is {}", a);
    for i in 1..=5 {
        println!("{}", i);
    }
    let a = Complex { re: 2.1, im: -1.2 };
    let b = Complex::new(11.1, 22.2);
    let result = a + b;

    println!("{} + {}i", result.re, result.im);

    let x = '中';
    println!("字符'中'占用了{}字节的内存大小", std::mem::size_of_val(&x));
    let t = true;

    let _f: bool = false; // 使用类型标注,显式指定f的类型

    if t {
        println!("这是段毫无意义的代码");
    }
    println!("函数返回值 {}", add_with_extra(5, 6));
    println!("函数返回值 {}", add(5, 6));
    let arr = [1, 2, 3];
    for v in arr {
        println!("{}", v);
    }
    // 语法糖 字符串切片
    let s = "hello world";
    println!("{}", &s[..5]);
    println!("{}", &s[..]);

    let wordindex = world(&s);
    println!("{}", wordindex);
    tt()
}
fn add_with_extra(x: i32, y: i32) -> i32 {
    let x = x + 1; // 语句
    let y = y + 5; // 语句
    x + y // 表达式
}

fn add(i: i32, j: i32) -> i32 {
    i + j
}
// pub mod base;
// 字符串切片
fn world(e: &str) -> &str {
    let bytes = e.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &e[..i];
        }
    }
    &e[..]
}

fn tt() {
    let mut v: Vec<u32> = Vec::new();
    // let s = vec![1, 2, 3];
    v.push(1);
    v.push(2);
    v.push(3);
    println!("{}", v.len());
    println!("{}", v[2]);
    for i in &mut v {
        println!("{}", (*i + 1));
    }

    let s1 = String::from("2022");
    let s2 = String::from("1116");
    // let s3 = s1 + &s2;

    // let s3 = s1 + s2;
    // println!("{}", s3);
    // println!("{}", s1);
    // println!("{}", s2);

    let s = format!("{} {}", s1, s2);
    println!("{}", s);
    println!("{}", s1);
    println!("{}", s2);
    let mut sco = HashMap::new();
    sco.insert(&s1, 10);
    println!("{:?}", sco.get(&s1));
}
